package cc.vv.btpartyjob.jnimodule.activity

import android.content.Intent
import android.view.View
import cc.vv.baselibrary.bean.base.BaseResponseObj
import cc.vv.baselibrary.http.BaseHttpRequest
import cc.vv.baselibrary.util.LKToastUtil
import cc.vv.baselibrary.view.BaseTopBarView
import cc.vv.btpartyjob.jnimodule.R
import cc.vv.btpartyjob.jnimodule.adapter.CunTreeAdapter
import cc.vv.btpartyjob.jnimodule.api.JniModuleAPI
import cc.vv.btpartyjob.jnimodule.binder.BaseBinder
import cc.vv.btpartyjob.jnimodule.delegate.VillageCunActivityDelegate
import cc.vv.btpartyjob.jnimodule.globle.IntentJniKey
import cc.vv.btpartyjob.jnimodule.moudel.bean.cun.CunBean
import cc.vv.btpartyjob.jnimodule.moudel.bean.cun.ZhenBean
import cc.vv.btpartyjob.jnimodule.moudel.bean.tree.Node
import cc.vv.btpartyjob.jnimodule.moudel.bean.tree.TreeListViewAdapter
import cc.vv.btpartyjob.jnimodule.moudel.bean.tree.ZtreeNodesBean
import cc.vv.btpartyjob.jnimodule.moudel.req.OrgRequestObj
import cc.vv.btpartyjob.jnimodule.moudel.resp.CunResponseObj
import cc.vv.mvp.activity.BaseActivityMVP
import cc.vv.mvp.binder.BaseDataBinder

/**
 * 行政村组织机构
 * Created by Chen.qi on 2018/10/30.
 */
class VillageCunActivity : BaseActivityMVP<VillageCunActivityDelegate>() {
    private var mAdapter: CunTreeAdapter? = null
    private val list = ArrayList<Node>()
    /**
     * type类型
     *  1:区
     *
     *  2：镇
     *
     *  3：村
     */
    private var showType: String? = null

    /**
     * chooseType类型
     *  1:区
     *
     *  2：镇
     *
     *  3：村
     */
    private var chooseType: Int = 2

    override fun initData() {
        viewDelegate?.setTopBarTitle("行政村查询")
        try {
            chooseType = intent.getIntExtra(IntentJniKey.INTENT_KEY_ORG_CHOOSE, 0)
        } catch (e: Exception) {
        }
        mAdapter = CunTreeAdapter(viewDelegate?.getListView(), this, list, 1, chooseType)
        viewDelegate?.getListView()?.adapter = mAdapter
        getAllOrgData()
    }


    /**
     * 获取所有村、镇组织结构数据
     */
    private fun getAllOrgData() {
        val path = JniModuleAPI.cunTree
        val params = OrgRequestObj()
        BaseHttpRequest.getRequest(path, params, CunResponseObj::class.java, true)
    }

    override fun bindEvenListener() {
        viewDelegate?.setTitleBackBtnOnClick(object : BaseTopBarView.EventInterface {
            //返回
            override fun leftOnClick() {
                finish()
            }

            override fun leftCloseClick() {}
            override fun rightOnClick() {
            }
        })


        mAdapter?.setOnTreeNodeClickListener(object : TreeListViewAdapter.OnTreeNodeClickListener {
            override fun onClick(node: Node?, position: Int) {
                if (node?.isLeaf!!) {
                    if (chooseType == 0) {
                        for (i in list.indices) {
                            list[i].ztreeNodesBean.isCheck = node.id == list[i].id
                        }
                    } else if (chooseType == 1) {
                        //只可选镇
                        if (node.ztreeNodesBean.type == "1") {
                            for (i in list.indices) {
                                list[i].ztreeNodesBean.isCheck = node.id == list[i].id
                            }
                        } else {
                            LKToastUtil.showToastShort("只可选则镇")
                        }
                    } else if (chooseType == 2) {
                        //只可选村
                        if (node.ztreeNodesBean.type == "2") {
                            for (i in list.indices) {
                                list[i].ztreeNodesBean.isCheck = node.id == list[i].id
                            }
                        } else {
                            LKToastUtil.showToastShort("只可选则村")
                        }
                    }
                    mAdapter?.notifyDataSetChanged()
                }

            }
        })


        viewDelegate?.setOnClickListener(View.OnClickListener {
            //确定
            var isCheck = false
            var orgId: String? = ""
            var orgName: String? = ""
            var orgPath: String? = ""
            for (i in list.indices) {
                val ztreeNodesBean = list[i].ztreeNodesBean
                if (ztreeNodesBean?.isCheck!!) {
                    orgId = ztreeNodesBean.id
                    orgName = ztreeNodesBean.name
                    orgPath = ztreeNodesBean.path
                    isCheck = true
                    break
                }
            }

            if (!isCheck) {
                LKToastUtil.showToastShort("请选择组织")
                return@OnClickListener
            }
            val intent = Intent()
            intent.putExtra(IntentJniKey.INTENT_KEY_ORGAN_ID, orgId)
            intent.putExtra(IntentJniKey.INTENT_KEY_ORGAN_NAME, orgName)

            setResult(101, intent)
            finish()
        }, R.id.rl_orGan_submit)
    }


    private fun getOrgName(id: String): String {
        val name: String? = (0 until list.size)
                .firstOrNull { id == list[it].id }
                ?.let { list[it].ztreeNodesBean.name }
        return name + " "
    }

    override fun getDelegateClass(): Class<VillageCunActivityDelegate> {
        return VillageCunActivityDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return BaseBinder()


    }

    override fun getData(obj: BaseResponseObj<*>?) {
        super.getData(obj)
        when (obj) {
            is CunResponseObj -> {
                //所有宝塔区数据
                val data = obj.data
                for (i in 0 until data.size) {
                    val baoTaBean = data[i]
                    val quId = baoTaBean.id
                    val quName = baoTaBean.villageName
                    val quParentId = "0"
                    val path = baoTaBean.path

                    val split = path?.split("/")
                    var type: String? = null
                    when {
                        split?.size == 1 -> type = "0"
                        split?.size == 2 -> type = "1"
                        split?.size == 3 -> type = "2"
                    }
                    //镇子数据
                    val zhenChild = baoTaBean?.child
                    val node = Node(quId, quParentId, ZtreeNodesBean(quId, quParentId, quName, type, path))
                    list.add(node)
                    getZhen(zhenChild)
                }
                //过滤逻辑
                val listFilter = ArrayList<Node>()
                listFilter.addAll(list)
                mAdapter?.setNodeData(listFilter, 2)
                mAdapter?.notifyDataSetChanged()
            }
        }
    }


    /**
     * 镇数据
     */
    private fun getZhen(children: ArrayList<ZhenBean>?) {
        if (null != children && children.size > 0) {
            for (i in 0 until children.size) {
                //镇数据
                val objBean = children[i]
                val jieDaoId = objBean.id
                val jieDaoName = objBean.villageName
                val jieDaoParentId = objBean.parentId
                val path = objBean.path

                val split = path?.split("/")
                var type: String? = null
                when {
                    split?.size == 1 -> type = "0"
                    split?.size == 2 -> type = "1"
                    split?.size == 3 -> type = "2"
                }


                val node = Node(jieDaoId, jieDaoParentId, ZtreeNodesBean(jieDaoId, jieDaoParentId, jieDaoName, type, path))
                list.add(node)

                //村数据
                val cun = objBean.child
                getCun(cun)
            }
        }
    }

    /**
     * 获取社区数据
     */
    private fun getCun(children: ArrayList<CunBean>?) {
        if (null != children && children.size > 0) {
            for (i in 0 until children.size) {
                //存数据
                val objBean = children[i]
                val sheQuId = objBean.id
                val sheQuName = objBean.villageName
                val sheQuParentId = objBean.parentId
                val path = objBean.path

                val split = path?.split("/")
                var type: String? = null
                when {
                    split?.size == 1 -> type = "0"
                    split?.size == 2 -> type = "1"
                    split?.size == 3 -> type = "2"
                }
                val node = Node(sheQuId, sheQuParentId, ZtreeNodesBean(sheQuId, sheQuParentId, sheQuName, type, path))
                list.add(node)
            }
        }
    }


    override fun getBadCode(obj: BaseResponseObj<*>?) {
        super.getBadCode(obj)
    }
}